import 'package:flutter/material.dart';

///  ***************************************************************************
///  轮训图片View
///  ***************************************************************************

class SlideView<T extends SlideImp> extends StatefulWidget {
  List<T> list;

  SlideCallBack slideCallBack;

  SlideView(this.list, this.slideCallBack) {
    if (slideCallBack == null) {
      slideCallBack = SlideCallBackImp();
    }
  }

  @override
  State<StatefulWidget> createState() {
    return new _SlideViewState();
  }
}

class _SlideViewState extends State<SlideView>
    with SingleTickerProviderStateMixin {
  TabController _tabController;

  @override
  void initState() {
    super.initState();
    int length = widget.list == 0 ? 0 : widget.list.length;
    _tabController = new TabController(length: length, vsync: this);
  }

  @override
  void dispose() {
    _tabController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return TabBarView(
      controller: _tabController,
      children: initListWiget(),
    );
  }

  List<Widget> initListWiget() {
    List<Widget> items = [];
    if (widget.list == null || widget.list.length == 0) {
      return items;
    }

    for (SlideImp item in widget.list) {
      items.add(initDetector(item));
    }
    return items;
  }

  GestureDetector initDetector(SlideImp slideImp) {
    return GestureDetector(
      onTap: () {
        widget.slideCallBack.handOnItemClick(slideImp);
      },
      child: AspectRatio(
        aspectRatio: 0.5,
        child: new Stack(
          children: <Widget>[
            Image.network(
              slideImp.slideImagePath(),
              fit: BoxFit.cover,
              width: 1000.0,
            ),
            Align(
              alignment: FractionalOffset.bottomCenter,
              child: new Container(
                width: 1000.0,
                color: Color(0x50000000),
                padding: EdgeInsets.all(5.0),
                child: new Text(
                  slideImp.slideTitle(),
                  style: TextStyle(color: Colors.white, fontSize: 15.0),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

abstract class SlideImp {
  String slideImagePath();

  String slideTitle();

  String slideUrl();
}

abstract class SlideCallBack {
  handOnItemClick(SlideImp item);
}

class SlideCallBackImp implements SlideCallBack {
  @override
  handOnItemClick(SlideImp item) {}
}
